package cn.lh.system.service.impl;

import cn.lh.system.domain.Role;
import cn.lh.system.mapper.RoleMapper;
import cn.lh.system.service.IRoleService;
import cn.lh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void add(Role role) {
        super.add(role);
        List<Long> menuIds = role.getMenus();
        if (menuIds.size() > 0){
            roleMapper.savaRoleMenus(role.getId(), menuIds);
        }
        List<Long> permissionIds = role.getPermissions();
        if (permissionIds.size() > 0) {
            roleMapper.saveRolePermissions(role.getId(), permissionIds);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        roleMapper.removeRoleMenuByRoleId(id);
        roleMapper.removeRolePermissionByRoleId(id);
        super.del(id);
    }

    @Override
    public void update(Role role) {
        super.update(role);
        roleMapper.removeRoleMenuByRoleId(role.getId());
        roleMapper.removeRolePermissionByRoleId(role.getId());
        List<Long> menuIds = role.getMenus();
        if (menuIds.size() > 0){
            roleMapper.savaRoleMenus(role.getId(), menuIds);
        }
        List<Long> permissionIds = role.getPermissions();
        if (permissionIds.size() > 0) {
            roleMapper.saveRolePermissions(role.getId(), permissionIds);
        }
    }
}
